var app = getApp();
var sliderWidth = 100;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    tabs: ["登录", "注册"],
    activeIndex: 0,
    sliderOffset: 0,
    sliderLeft: 0
  },
  onLoad: function () {
    var ID = wx.getStorageSync('userID')
    console.log('ID:'+ID)
    if(ID!=''){
      wx.switchTab({
        url: '/pages/home/home',
        success: function (res) { },
        fail: function (res) { },
        complete: function (res) { },
      })
    }
    var that = this;
    wx.getSystemInfo({
      success: function (res) {
        that.setData({
          sliderLeft: (res.windowWidth / that.data.tabs.length - sliderWidth) / 2,
          sliderOffset: res.windowWidth / that.data.tabs.length * that.data.activeIndex
        });
      }
    });
  },
  tabClick: function (e) {
    this.setData({
      sliderOffset: e.currentTarget.offsetLeft,
      activeIndex: e.currentTarget.id
    });
  },
  signup:function(e){
    console.log(e.detail.value);
    var nickname=e.detail.value.nickname;
    var phone=e.detail.value.phone;
    var password=e.detail.value.password;
    var that=this;
    wx.request({
      url: 'https://55300249.qcloud.la/JapariFriends/register',
      data: {
        userId:phone,
        nickName:nickname,
        password:password
      },
      method: 'get',
      success: function(res) {
        console.log(res);
        if(res.data=='failure'){
          wx.showModal({
            title: '注册失败',
            content: '手机号已被占用',
            showCancel: false,
            confirmText: '重试'
          })
          return;
        }
        that.setData({
          sliderOffset: 0,
          activeIndex: 0
        });
      },
      fail: function(res) {},
      complete: function(res) {},
    })
  },
  login:function(e){
    var that=this
    var phone = e.detail.value.phone;
    var password = e.detail.value.password;
    wx.request({
      url: 'https://55300249.qcloud.la/JapariFriends/login',
      data: {
        userId:phone,
        password:password
      },
      method: 'get',
      success: function(res) {
        if(res.statusCode==500){
          wx.showModal({
            title: '登录失败',
            content: '账号或密码错误',
            showCancel: false,
            confirmText: '重试'
          })
          return;
        }
        
        wx.request({
          url: 'https://55300249.qcloud.la/JapariFriends/returnRecord',
          data: {
            userId: phone,
            rentType: 0
          },
          method: 'get',
          success: function (res) {
            if (res.data.length >= 1){
              wx.showModal({
                title: '还书提醒',
                content: '您有 ' + res.data.length+' 本书未归还，记得到图书馆归还哟',
                showCancel: false,
                confirmText: '我知道了'
              })
            }
          },
        })


        wx.setStorageSync('userID', phone),
        wx.switchTab({
          url: '/pages/home/home',
          success: function(res) {},
          fail: function(res) {},
          complete: function(res) {},
        })
      },
      fail: function(res) {
        console.log('fail!');
        console.log(res);
      },
      complete: function(res) {},
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})